/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainCheckRecord;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanContentExt;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.service.IMaintainPlanContentExtService;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.MaintainCheckRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.repair.entity.MaintainCheckHistory;
import org.springblade.modules.repair.vo.MaintainCheckHistoryVO;
import org.springblade.modules.repair.wrapper.MaintainCheckHistoryWrapper;
import org.springblade.modules.repair.service.IMaintainCheckHistoryService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 点检内容历史记录 控制器
 *
 * @author BladeX
 * @since 2021-05-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/maintain/check/history")
@Api(value = "点检内容历史记录", tags = "点检内容历史记录接口")
public class MaintainCheckHistoryController extends BladeController {

	private final IMaintainCheckHistoryService maintainCheckHistoryService;
	private final MaintainCheckRecordService checkRecordService;
	private final IMaintainPlanContentService planContentService;
	private final IMaintainPlanContentExtService planContentExtService;

	/**
	 * 自定义分页 点检内容历史记录
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入maintainCheckHistory")
	public R<IPage<MaintainCheckHistoryVO>> page(MaintainCheckHistoryVO maintainCheckHistory, Query query) {
		IPage<MaintainCheckHistoryVO> pages = maintainCheckHistoryService.selectMaintainCheckHistoryPage(Condition.getPage(query), maintainCheckHistory);
		return R.data(pages);
	}

	/**
	 * 新增 点检内容历史记录
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入maintainCheckHistory")
	public R save(@Valid @RequestBody MaintainCheckHistory maintainCheckHistory) {
		return R.status(maintainCheckHistoryService.save(maintainCheckHistory));
	}

	/**
	 * 修改 点检内容历史记录
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入maintainCheckHistory")
	public R update(@Valid @RequestBody MaintainCheckHistory maintainCheckHistory) {
		return R.status(maintainCheckHistoryService.updateDetail(maintainCheckHistory));
	}

	/**
	 * 修改
	 */
	@PutMapping("/{id}")
	public R updateById(@PathVariable Long id,@Valid @RequestBody MaintainCheckHistory maintainCheckHistory) {
		maintainCheckHistory.setId(id);
		return R.status(maintainCheckHistoryService.updateById(maintainCheckHistory));
	}

	/**
	 * 新增或修改 检查项历史操作记录
	 * @param entity
	 * @return
	 */
	@ApiLog("工单操作记录")
	@PostMapping("/add/check/record")
	public R<Boolean> addCheckRecord(@RequestBody MaintainCheckRecord entity){
		MaintainPlanContent planContent = planContentService.getById(entity.getPlanId());
		if(!planContent.getStatus().equals(TaskStatusEnum.BY_PROCESSED.getStatus()) && planContent.getTypeStatus().equals(MaintainPlanContentEnum.MAINTAIN_BY.getId())){
			throw new ServiceException("工单状态已变更，请刷新页面");
		}
		MaintainPlanContentExt planContentExt = planContentExtService.getOne(Wrappers.<MaintainPlanContentExt>lambdaQuery().eq(MaintainPlanContentExt::getPlanId,entity.getPlanId()));
		if(planContent.getTypeStatus().equals(MaintainPlanContentEnum.MAINTAIN_WX.getId())){
			if(!planContent.getStatus().equals(TaskStatusEnum.WX_UNDER_WAY.getStatus())){
				if(planContent.getStatus().equals(TaskStatusEnum.WX_SIGN_FINISH.getStatus())){
					long betweenHour = DateUtil.between(planContentExt.getPlanEndTime(),DateUtil.date(), DateUnit.HOUR);
					if(betweenHour > 24){
						throw new ServiceException("工单状态已变更，请刷新页面");
					}
				}else {
					throw new ServiceException("工单状态已变更，请刷新页面");
				}
			}
		}
		return R.data(checkRecordService.addCheckRecord(entity));
	}

	/**
	 * 删除检查项历史操作记录
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/delete/check/record")
	public R<Boolean> deleteCheckRecord(@RequestParam String ids,Long planId){
		MaintainPlanContent planContent = planContentService.getById(planId);
		if(!planContent.getStatus().equals(TaskStatusEnum.BY_PROCESSED.getStatus())){
			throw new ServiceException("工单状态已变更，请刷新页面");
		}
		List<Long> idList = Func.toLongList(ids);
		idList.forEach(e -> {
			MaintainCheckRecord checkRecord = checkRecordService.getById(e);
			if(AuthUtil.getUserId().equals(checkRecord.getUserId())){
				checkRecordService.removeById(checkRecord.getId());
			}
		});
		return R.data(Boolean.TRUE);

	}

	/**
	 * @Author lsay
	 * @Description {
	 *   批量更新保养历史检查项
	 * }
	 * @Date 9:51 2023/2/21
	 * @Param
	 * @return
	 **/
	@PostMapping("/update/batch")
	public R<Boolean> updateBatchCheckContent(@RequestBody List<MaintainCheckHistory> entityList){
		if(ObjectUtil.isEmpty(entityList)){
			throw new ServiceException("工作项不可为空");
		}
		Long planId = entityList.get(0).getPlanId();
		MaintainPlanContent planContent = planContentService.getById(planId);
		if(!planContent.getStatus().equals(TaskStatusEnum.BY_PROCESSED.getStatus())){
			throw new ServiceException("工单状态已变更，请刷新页面");
		}
		return R.status(maintainCheckHistoryService.updateBatchById(entityList));
	}




}




























